/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI.NVQL;

import GUI.NVQL.frXemDanhSachHoaDonCuaKhachHang;
import DAO.CTHoaDonDao;
import GUI.TOOL.ExcelOpener;
import DAO.HoaDonDao;
import DAO.KhachHangDao;
import GUI.TOOL.XuatExcel;
import GUI.MainForm;
import POJO.CTHoaDon;
import POJO.HoaDon;
import POJO.KhachHang;
import java.beans.PropertyVetoException;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Kim
 */
public class frXemThongTinKhachHang extends javax.swing.JInternalFrame {
	
	private DefaultTableModel tbmodel1;
	private int TSD;// tong so dong trong bang hoa don
	private int THT; //trang hiện tại
	private int SD;// hien thi bao nhieu dong tron trang

	/**
	 * Creates new form frXemThongTinKhachHang
	 */
	public frXemThongTinKhachHang() {
		initComponents();
		LoadLaiDuLieu();
	}
	
	public void DefaultFormKhachHang() {
		tbmodel1 = new DefaultTableModel();

		//tạo các cột
		this.tbmodel1.addColumn("STT ");
		this.tbmodel1.addColumn("Mã khách hàng");
		this.tbmodel1.addColumn("Họ tên");
		this.tbmodel1.addColumn("Số điện thoại");
		this.tbmodel1.addColumn("Số lần mua");
		
	}
	
	private void LoadLaiDuLieu() {
		TSD = soDongDuLieu();
		SD = 20;
		THT = 0;
		load(SD, THT);
		setTxt();
	}
	
	public String KiemTraMa() {
		String str = "";
		if (jtfMaKhachHang.getText().equals("")) {
			str += "- Chưa nhập mã khách hàng ! \r\n";
		}
		return str;
	}
	
	public String KiemTraTen() {
		String str = "";
		if (jtfTenKH.getText().equals("")) {
			str += "- Chưa nhập tên khách hàng ! \r\n";
		}
		return str;
	}
	
	public String KiemTraDienThoai() {
		String str = "";
		if (jtfSoDienThoai.getText().equals("")) {
			str += "- Chưa nhập số điện thoại ! \r\n";
		}
		return str;
	}
	
	private KhachHang setKhachHang() {
		int ma = 0;
		if (!jtfMaKhachHang.getText().trim().equals("")) {
			
			try {
				ma = Integer.parseInt(this.jtfMaKhachHang.getText().trim());
				
			} catch (NumberFormatException e) {
				JOptionPane.showMessageDialog(this, "Lỗi nhập số", "Thông báo lỗi", JOptionPane.ERROR_MESSAGE);
				jtfMaKhachHang.requestFocus();
				return null;
			}
		}
		
		String TenKH = this.jtfTenKH.getText().trim();
		String DienThoai = this.jtfSoDienThoai.getText().trim();
		
		KhachHang kh = new KhachHang();
		kh.setMaKH(ma);
		kh.setTenKH(TenKH);
		kh.setDienThoai(DienThoai);
		return kh;
	}
	
	private void timKiemVaHienThi() {
		try {
			KhachHang kh = new KhachHang();
			
			kh = setKhachHang();
			if (kh == null) {
				return;
			}
			KhachHangDao dao = new KhachHangDao();
			
			ArrayList<KhachHang> dskh = new ArrayList<KhachHang>();
			tbmodel1 = new DefaultTableModel();
			DefaultFormKhachHang();
			dskh = dao.timKiemVoiSoLuongMua(kh);
			
			int i = 1;
			for (KhachHang kh1 : dskh) {
				
				Object[] items = new Object[]{
					i,
					kh1.getMaKH(),
					kh1.getTenKH(),
					kh1.getDienThoai(),
					kh1.getSoLuong()
				};
				
				i++;
				//int maLSP = sp.getMaLSP();

				//ArrayList<HoaDon> hd = HoaDonDao.layByMaKH(kh1.getMaKH());				
				tbmodel1.addRow(items);
				
			}
			jtDSKhachHang.setModel(tbmodel1);
			int[] va = {5, 20, 30, 20, 25};
			int[] pt = new int[5];
			for (int l = 0; l < 5; l++) {
				pt[l] = jtDSKhachHang.getWidth() * va[l] / 100;
				jtDSKhachHang.getColumnModel().getColumn(l).setPreferredWidth(pt[l]);
				
			}
			if (tbmodel1.getRowCount() > 0) {
				loadText(0);
			}
			
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, "Không tìm thấy");
		}
		
	}
	
	private void loadText(int dong) {
		
		int maKH = Integer.parseInt(this.tbmodel1.getValueAt(dong, 1).toString());
		String tenKH = (String) this.tbmodel1.getValueAt(dong, 2).toString();
		String dienThoai = this.tbmodel1.getValueAt(dong, 3).toString();
		int soLuongMua = Integer.parseInt(this.tbmodel1.getValueAt(dong, 4).toString());
		
		jtfMaKhachHang.setText(String.valueOf(maKH));
		jtfTenKH.setText(tenKH);
		jtfSoDienThoai.setText(String.valueOf(dienThoai));
		jtfSoLuongMua.setText(String.valueOf(soLuongMua));
	}

	/**
	 * This method is called from within the constructor to initialize the
	 * form. WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
       // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
       private void initComponents() {

              jPanel1 = new javax.swing.JPanel();
              jLabel5 = new javax.swing.JLabel();
              jPanel2 = new javax.swing.JPanel();
              jLabel1 = new javax.swing.JLabel();
              jLabel2 = new javax.swing.JLabel();
              jLabel3 = new javax.swing.JLabel();
              jLabel4 = new javax.swing.JLabel();
              jtfSoLuongMua = new javax.swing.JTextField();
              jtfSoDienThoai = new javax.swing.JTextField();
              jtfTenKH = new javax.swing.JTextField();
              jtfMaKhachHang = new javax.swing.JTextField();
              jbtnTim = new javax.swing.JButton();
              jPanel3 = new javax.swing.JPanel();
              jScrollPane1 = new javax.swing.JScrollPane();
              jtDSKhachHang = new javax.swing.JTable();
              jPanel9 = new javax.swing.JPanel();
              btDau = new javax.swing.JButton();
              btTruoc = new javax.swing.JButton();
              btCuoi = new javax.swing.JButton();
              btSau = new javax.swing.JButton();
              lbPhanTrang = new javax.swing.JLabel();
              jPanel10 = new javax.swing.JPanel();
              jbtnLapBaoCao2 = new javax.swing.JButton();
              jButton15 = new javax.swing.JButton();
              btXemDanhSachHoaDon = new javax.swing.JButton();

              setClosable(true);
              setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
              setIconifiable(true);
              setMaximizable(true);
              setResizable(true);

              jLabel5.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
              jLabel5.setText("XEM THÔNG TIN KHÁCH HÀNG");

              javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
              jPanel1.setLayout(jPanel1Layout);
              jPanel1Layout.setHorizontalGroup(
                     jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel1Layout.createSequentialGroup()
                            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel5)
                            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
              );
              jPanel1Layout.setVerticalGroup(
                     jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel1Layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(jLabel5)
                            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
              );

              jLabel1.setText("Mã khách hàng:");

              jLabel2.setText("Tên khách hàng:");

              jLabel3.setText("Số điện thoại:");

              jLabel4.setText("Số lần mua:");

              jtfSoLuongMua.setText("[count]");
              jtfSoLuongMua.setEnabled(false);
              jtfSoLuongMua.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            jtfSoLuongMuaActionPerformed(evt);
                     }
              });
              jtfSoLuongMua.addInputMethodListener(new java.awt.event.InputMethodListener() {
                     public void caretPositionChanged(java.awt.event.InputMethodEvent evt) {
                     }
                     public void inputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
                            jtfSoLuongMuaInputMethodTextChanged(evt);
                     }
              });
              jtfSoLuongMua.addKeyListener(new java.awt.event.KeyAdapter() {
                     public void keyReleased(java.awt.event.KeyEvent evt) {
                            jtfSoLuongMuaKeyReleased(evt);
                     }
              });
              jtfSoLuongMua.addVetoableChangeListener(new java.beans.VetoableChangeListener() {
                     public void vetoableChange(java.beans.PropertyChangeEvent evt)throws java.beans.PropertyVetoException {
                            jtfSoLuongMuaVetoableChange(evt);
                     }
              });

              jtfSoDienThoai.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            jtfSoDienThoaiActionPerformed(evt);
                     }
              });
              jtfSoDienThoai.addInputMethodListener(new java.awt.event.InputMethodListener() {
                     public void caretPositionChanged(java.awt.event.InputMethodEvent evt) {
                     }
                     public void inputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
                            jtfSoDienThoaiInputMethodTextChanged(evt);
                     }
              });
              jtfSoDienThoai.addKeyListener(new java.awt.event.KeyAdapter() {
                     public void keyReleased(java.awt.event.KeyEvent evt) {
                            jtfSoDienThoaiKeyReleased(evt);
                     }
              });
              jtfSoDienThoai.addVetoableChangeListener(new java.beans.VetoableChangeListener() {
                     public void vetoableChange(java.beans.PropertyChangeEvent evt)throws java.beans.PropertyVetoException {
                            jtfSoDienThoaiVetoableChange(evt);
                     }
              });

              jtfTenKH.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            jtfTenKHActionPerformed(evt);
                     }
              });
              jtfTenKH.addInputMethodListener(new java.awt.event.InputMethodListener() {
                     public void caretPositionChanged(java.awt.event.InputMethodEvent evt) {
                     }
                     public void inputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
                            jtfTenKHInputMethodTextChanged(evt);
                     }
              });
              jtfTenKH.addKeyListener(new java.awt.event.KeyAdapter() {
                     public void keyReleased(java.awt.event.KeyEvent evt) {
                            jtfTenKHKeyReleased(evt);
                     }
              });
              jtfTenKH.addVetoableChangeListener(new java.beans.VetoableChangeListener() {
                     public void vetoableChange(java.beans.PropertyChangeEvent evt)throws java.beans.PropertyVetoException {
                            jtfTenKHVetoableChange(evt);
                     }
              });

              jbtnTim.setText("Tìm");
              jbtnTim.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            jbtnTimActionPerformed(evt);
                     }
              });

              javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
              jPanel2.setLayout(jPanel2Layout);
              jPanel2Layout.setHorizontalGroup(
                     jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel2Layout.createSequentialGroup()
                            .addGap(145, 145, 145)
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                   .addComponent(jLabel1)
                                   .addComponent(jLabel2)
                                   .addComponent(jLabel3)
                                   .addComponent(jLabel4))
                            .addGap(32, 32, 32)
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                   .addComponent(jtfSoLuongMua)
                                   .addComponent(jtfSoDienThoai)
                                   .addComponent(jtfTenKH, javax.swing.GroupLayout.Alignment.LEADING)
                                   .addComponent(jtfMaKhachHang, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jbtnTim)
                            .addContainerGap())
              );
              jPanel2Layout.setVerticalGroup(
                     jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel2Layout.createSequentialGroup()
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                   .addComponent(jtfMaKhachHang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                   .addComponent(jLabel1))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                   .addComponent(jtfTenKH, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                   .addComponent(jLabel2))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                   .addComponent(jtfSoDienThoai, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                   .addComponent(jLabel3))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                   .addComponent(jtfSoLuongMua, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                   .addComponent(jLabel4)
                                   .addComponent(jbtnTim))
                            .addGap(0, 0, Short.MAX_VALUE))
              );

              jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Danh sách khách hàng"));

              jtDSKhachHang.setModel(new javax.swing.table.DefaultTableModel(
                     new Object [][] {

                     },
                     new String [] {
                            "STT", "Mã khách hàng", "Họ tên", "Số điện thoại", "Số lần mua"
                     }
              ));
              jtDSKhachHang.addMouseListener(new java.awt.event.MouseAdapter() {
                     public void mouseClicked(java.awt.event.MouseEvent evt) {
                            jtDSKhachHangMouseClicked(evt);
                     }
              });
              jScrollPane1.setViewportView(jtDSKhachHang);

              btDau.setText("|<");
              btDau.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            btDauActionPerformed(evt);
                     }
              });

              btTruoc.setText("<");
              btTruoc.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            btTruocActionPerformed(evt);
                     }
              });

              btCuoi.setText(">|");
              btCuoi.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            btCuoiActionPerformed(evt);
                     }
              });

              btSau.setText(">");
              btSau.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            btSauActionPerformed(evt);
                     }
              });

              lbPhanTrang.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
              lbPhanTrang.setText("0");

              javax.swing.GroupLayout jPanel9Layout = new javax.swing.GroupLayout(jPanel9);
              jPanel9.setLayout(jPanel9Layout);
              jPanel9Layout.setHorizontalGroup(
                     jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel9Layout.createSequentialGroup()
                            .addContainerGap(221, Short.MAX_VALUE)
                            .addComponent(btDau)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(btTruoc)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(lbPhanTrang)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(btSau)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(btCuoi)
                            .addContainerGap(222, Short.MAX_VALUE))
              );
              jPanel9Layout.setVerticalGroup(
                     jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel9Layout.createSequentialGroup()
                            .addGap(0, 0, Short.MAX_VALUE)
                            .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                   .addComponent(btDau)
                                   .addComponent(btTruoc)
                                   .addComponent(btCuoi)
                                   .addComponent(btSau)
                                   .addComponent(lbPhanTrang)))
              );

              javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
              jPanel3.setLayout(jPanel3Layout);
              jPanel3Layout.setHorizontalGroup(
                     jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 642, Short.MAX_VALUE)
                     .addComponent(jPanel9, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
              );
              jPanel3Layout.setVerticalGroup(
                     jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel3Layout.createSequentialGroup()
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 226, Short.MAX_VALUE)
                            .addGap(0, 0, 0)
                            .addComponent(jPanel9, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
              );

              jPanel10.setBorder(javax.swing.BorderFactory.createTitledBorder("Chức năng"));

              jbtnLapBaoCao2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
              jbtnLapBaoCao2.setText("Lập báo cáo");
              jbtnLapBaoCao2.setToolTipText("gọi lập phiếu nhập kho");
              jbtnLapBaoCao2.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            jbtnLapBaoCao2ActionPerformed(evt);
                     }
              });

              jButton15.setText("Lưu ra Excel");
              jButton15.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            jButton15ActionPerformed(evt);
                     }
              });

              btXemDanhSachHoaDon.setText("Xem danh sách hóa đơn");
              btXemDanhSachHoaDon.setEnabled(false);
              btXemDanhSachHoaDon.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            btXemDanhSachHoaDonActionPerformed(evt);
                     }
              });

              javax.swing.GroupLayout jPanel10Layout = new javax.swing.GroupLayout(jPanel10);
              jPanel10.setLayout(jPanel10Layout);
              jPanel10Layout.setHorizontalGroup(
                     jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel10Layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(jbtnLapBaoCao2)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jButton15, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btXemDanhSachHoaDon)
                            .addContainerGap())
              );
              jPanel10Layout.setVerticalGroup(
                     jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jbtnLapBaoCao2)
                            .addComponent(jButton15)
                            .addComponent(btXemDanhSachHoaDon))
              );

              javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
              getContentPane().setLayout(layout);
              layout.setHorizontalGroup(
                     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(layout.createSequentialGroup()
                            .addGap(0, 0, 0)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                   .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                   .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                   .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                   .addComponent(jPanel10, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGap(0, 0, 0))
              );
              layout.setVerticalGroup(
                     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(layout.createSequentialGroup()
                            .addGap(0, 0, 0)
                            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(0, 0, 0)
                            .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(0, 0, 0)
                            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGap(0, 0, 0)
                            .addComponent(jPanel10, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(0, 0, 0))
              );

              pack();
       }// </editor-fold>//GEN-END:initComponents

       private void jtfSoLuongMuaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtfSoLuongMuaActionPerformed
		// TODO add your handling code here:
       }//GEN-LAST:event_jtfSoLuongMuaActionPerformed
	
       private void jtfSoLuongMuaInputMethodTextChanged(java.awt.event.InputMethodEvent evt) {//GEN-FIRST:event_jtfSoLuongMuaInputMethodTextChanged
		// TODO add your handling code here:
       }//GEN-LAST:event_jtfSoLuongMuaInputMethodTextChanged
	
       private void jtfSoLuongMuaKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtfSoLuongMuaKeyReleased
		// TODO add your handling code here:
       }//GEN-LAST:event_jtfSoLuongMuaKeyReleased
	
       private void jtfSoLuongMuaVetoableChange(java.beans.PropertyChangeEvent evt)throws java.beans.PropertyVetoException {//GEN-FIRST:event_jtfSoLuongMuaVetoableChange
		// TODO add your handling code here:
       }//GEN-LAST:event_jtfSoLuongMuaVetoableChange
	
       private void jtfSoDienThoaiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtfSoDienThoaiActionPerformed
		// TODO add your handling code here:
       }//GEN-LAST:event_jtfSoDienThoaiActionPerformed
	
       private void jtfSoDienThoaiInputMethodTextChanged(java.awt.event.InputMethodEvent evt) {//GEN-FIRST:event_jtfSoDienThoaiInputMethodTextChanged
		// TODO add your handling code here:
       }//GEN-LAST:event_jtfSoDienThoaiInputMethodTextChanged
	
       private void jtfSoDienThoaiKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtfSoDienThoaiKeyReleased
		// TODO add your handling code here:
       }//GEN-LAST:event_jtfSoDienThoaiKeyReleased
	
       private void jtfSoDienThoaiVetoableChange(java.beans.PropertyChangeEvent evt)throws java.beans.PropertyVetoException {//GEN-FIRST:event_jtfSoDienThoaiVetoableChange
		// TODO add your handling code here:
       }//GEN-LAST:event_jtfSoDienThoaiVetoableChange
	
       private void jtfTenKHActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtfTenKHActionPerformed
		// TODO add your handling code here:
       }//GEN-LAST:event_jtfTenKHActionPerformed
	
       private void jtfTenKHInputMethodTextChanged(java.awt.event.InputMethodEvent evt) {//GEN-FIRST:event_jtfTenKHInputMethodTextChanged
		// TODO add your handling code here:
       }//GEN-LAST:event_jtfTenKHInputMethodTextChanged
	
       private void jtfTenKHKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtfTenKHKeyReleased
		// TODO add your handling code here:
		//       vbtSua();
       }//GEN-LAST:event_jtfTenKHKeyReleased
	
       private void jtfTenKHVetoableChange(java.beans.PropertyChangeEvent evt)throws java.beans.PropertyVetoException {//GEN-FIRST:event_jtfTenKHVetoableChange
		// TODO add your handling code here:
       }//GEN-LAST:event_jtfTenKHVetoableChange
	
       private void jbtnLapBaoCao2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnLapBaoCao2ActionPerformed
		// TODO add your handling code here:
		hienPhanTrang();
		load(SD, 1);
		//loadKhachHang();
       }//GEN-LAST:event_jbtnLapBaoCao2ActionPerformed
	
       private void btDauActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btDauActionPerformed
		// TODO add your handling code here:
		// ve dau tien
		THT = 0;
		load(SD, THT);
		setTxt();
       }//GEN-LAST:event_btDauActionPerformed
	
       private void btTruocActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btTruocActionPerformed
		// TODO add your handling code here:
		//ve trang truoc
		if (THT != 0) {
			THT -= 1;
			load(SD, THT);
		}
		setTxt();
       }//GEN-LAST:event_btTruocActionPerformed
	
       private void btCuoiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCuoiActionPerformed
		// TODO add your handling code here:
		// ve cuoi

		THT = TSD / SD;
		load(SD, THT);
		
		setTxt();
       }//GEN-LAST:event_btCuoiActionPerformed
	private void setTxt() {
		lbPhanTrang.setText(Integer.toString(THT + 1) + "/" + Integer.toString(TSD / SD + 1));
	}
	
	private int soDongDuLieu() {
		int tmp = 0;
		KhachHangDao dao = new KhachHangDao();
		try {
			tmp = dao.laySoDong();
		} catch (Exception e) {
		}
		return tmp;
	}
	
	public void load(int soDong, int trangHienTai) {
		DefaultFormKhachHang();
		
		TSD = soDongDuLieu();
		
		ArrayList<KhachHang> dsKhachHang = null;
		try {
			dsKhachHang = KhachHangDao.layDanhSachPhanTrang(soDong, trangHienTai);
			
		} catch (Exception e) {
		}
		
		HoaDonDao hdDao = new HoaDonDao();
		CTHoaDonDao cthdDao = new CTHoaDonDao();
		int i = 1;
		for (KhachHang kh : dsKhachHang) {
			int soLanMua = hdDao.laySoLanMua(kh.getMaKH());
			
			Object[] items = new Object[]{
				i,
				kh.getMaKH(),
				kh.getTenKH(),
				kh.getDienThoai(),
				soLanMua
			};
			
			i++;
			//int maLSP = sp.getMaLSP();
			tbmodel1.addRow(items);
			
		}
		jtDSKhachHang.setModel(tbmodel1);
		
		int[] va = {5, 20, 30, 20, 25};
		int[] pt = new int[5];
		for (int l = 0; l < 5; l++) {
			pt[l] = jtDSKhachHang.getWidth() * va[l] / 100;
			jtDSKhachHang.getColumnModel().getColumn(l).setPreferredWidth(pt[l]);
			
		}
	}
       private void btSauActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSauActionPerformed
		// TODO add your handling code here:
		if (THT < TSD / SD) {
			THT += 1;
			load(SD, THT);
		}
		setTxt();
       }//GEN-LAST:event_btSauActionPerformed
	
    private void jtDSKhachHangMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jtDSKhachHangMouseClicked
	    // TODO add your handling code here:

	    int dong = this.jtDSKhachHang.getSelectedRow();
	    if (dong != -1) {
		    int soLanMua = Integer.parseInt(this.tbmodel1.getValueAt(dong, 4).toString());
		    if (soLanMua != 0) {
			    btXemDanhSachHoaDon.setEnabled(true);
		    } else {
			    btXemDanhSachHoaDon.setEnabled(false);
			    
		    }
		    loadText(dong);
	    }
    }//GEN-LAST:event_jtDSKhachHangMouseClicked
	private void anPhanTrang() {
		btDau.setVisible(false);
		btTruoc.setVisible(false);
		btSau.setVisible(false);
		btCuoi.setVisible(false);
		lbPhanTrang.setVisible(false);
	}
	
	private void hienPhanTrang() {
		btDau.setVisible(true);
		btTruoc.setVisible(true);
		btSau.setVisible(true);
		btCuoi.setVisible(true);
		lbPhanTrang.setVisible(true);
		
	}
    private void jbtnTimActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnTimActionPerformed
	    // TODO add your handling code here:
	    anPhanTrang();
	    timKiemVaHienThi();
    }//GEN-LAST:event_jbtnTimActionPerformed
	
    private void jButton15ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton15ActionPerformed
	    // TODO add your handling code here:
	    try {
		    XuatExcel exp = new XuatExcel();
		    File file = new File("SoLanKhachHangMua.xls");
		    exp.exportTable(jtDSKhachHang, file);
		    ExcelOpener opn = new ExcelOpener();
		    opn.openTable(file);
	    } catch (IOException ex) {
		    System.out.println(ex.getMessage());
		    ex.printStackTrace();
	    }
    }//GEN-LAST:event_jButton15ActionPerformed
	
    private void btXemDanhSachHoaDonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btXemDanhSachHoaDonActionPerformed
	    // TODO add your handling code here:
	    frXemDanhSachHoaDonCuaKhachHang fr = new frXemDanhSachHoaDonCuaKhachHang();
	    fr.setVisible(true);
	    int dong = jtDSKhachHang.getSelectedRow();
	    MainForm.jDesktopPane1.add(fr);
	    int maKH = Integer.parseInt(this.tbmodel1.getValueAt(dong, 1).toString());
	    
	    fr.setMaKhachHang(maKH);
	    fr.load();
	    try {
		    fr.setSelected(isSelected);
	    } catch (PropertyVetoException ex) {
		    Logger.getLogger(GUI.NVGH.frXemThongTinKhachHang.class.getName()).log(Level.SEVERE, null, ex);
	    }
	    
    }//GEN-LAST:event_btXemDanhSachHoaDonActionPerformed
       // Variables declaration - do not modify//GEN-BEGIN:variables
       private javax.swing.JButton btCuoi;
       private javax.swing.JButton btDau;
       private javax.swing.JButton btSau;
       private javax.swing.JButton btTruoc;
       private javax.swing.JButton btXemDanhSachHoaDon;
       private javax.swing.JButton jButton15;
       private javax.swing.JLabel jLabel1;
       private javax.swing.JLabel jLabel2;
       private javax.swing.JLabel jLabel3;
       private javax.swing.JLabel jLabel4;
       private javax.swing.JLabel jLabel5;
       private javax.swing.JPanel jPanel1;
       private javax.swing.JPanel jPanel10;
       private javax.swing.JPanel jPanel2;
       private javax.swing.JPanel jPanel3;
       private javax.swing.JPanel jPanel9;
       private javax.swing.JScrollPane jScrollPane1;
       private javax.swing.JButton jbtnLapBaoCao2;
       private javax.swing.JButton jbtnTim;
       private javax.swing.JTable jtDSKhachHang;
       private javax.swing.JTextField jtfMaKhachHang;
       private javax.swing.JTextField jtfSoDienThoai;
       private javax.swing.JTextField jtfSoLuongMua;
       private javax.swing.JTextField jtfTenKH;
       private javax.swing.JLabel lbPhanTrang;
       // End of variables declaration//GEN-END:variables
}
